<template>
  <div class="login">
    <!-- 这是login页面 -->
    <!-- 通过type（颜色） plain（）..配置按钮 -->

        <!--
        1.el-form整个大的表单组件,
          :model="form" 配置了model属性，
           label-width 控制整个表单的label宽度的
        2.el-form-item 一行，一个表单项
           label="活动名称" 设置label的文字，

        3.el-input 输入框组件
          v-model双向数据绑定，和form对象的属性，双向数据绑定
        -->

        <el-form ref='form' status-icon :rules="rules" :model="form" label-width="80px">
          <img class="meinv" src="../assets/avatar.jpg" alt="">
          <el-form-item label="用户名：" label-width="80px" prop='username'>
          <el-input @keyup.enter.native='login ' palceholder='请输入用户名'  v-model='form.username'></el-input>
        </el-form-item>
         <el-form-item label="密码：" prop='password'>
          <el-input @keyup.enter.native='login ' palceholder='请输入密码' type='password' v-model="form.password "></el-input>
        </el-form-item>
         <el-form-item>
           <el-button @click='login' class="loginbtn" type='primary'>登录</el-button>
           <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
  </div>
</template>

<script>
// 导入axios
import axios from 'axios'

export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      //   name: [
      // ],
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: ['blur', 'change'] },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: ['blur', 'change'] }

        ],
        password: [
          { required: true, message: '请输入用户名', trigger: ['blur', 'change'] },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: ['blur', 'change'] }

        ]
      }
    }
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    login () {
      this.$refs.form.validate((isValid) => {
        if (!isValid) return
        axios.post('http://localhost:8888/api/private/v1/login', this.form).then(res => {
          const { meta, data } = res.data
          if (meta.status === 200) {
            this.$message({
              showClose: true,
              message: meta.msg,
              type: 'success',
              duration: 2000
            })

            localStorage.setItem('token', data.token)

            this.$router.push('./index')
          } else {
            this.$message({
              showClose: true,
              message: meta.msg,
              type: 'error',
              duration: 2000
            })
          }
        })
      })
    }
  }

}
</script>

<style lang='less'>
 .login{
   width: 100%;
   height: 100%;
   background-color: #2d434c;
   overflow: hidden;

    .el-form{
      width: 400px;
      padding: 20px;
      padding-top: 80px;
      background-color: #fff;
      border-radius: 20px;
      margin:0 auto;
      margin-top: 200px;
      position: relative;
    }

    .loginbtn{
      margin-right: 70px;
    }

    .meinv{
      position: absolute;
      left: 50%;
      transform: translate(-50%);
      top: -70px;
      border-radius: 50%;
      border: 3px solid #fff;
    }
 }
</style>
